package com.mastercard.labs

class RewardPointService {
	def cardService
	def grailsApplication
    def serviceMethod() {

    }
	
	def updateRewardPoint(transaction){
		log.info "updateRewardPoint"
		def user = cardService.getUser(transaction.card)
		def calculatePoint = grailsApplication.config.rewards.formular
		def rpList = RewardPoint.findAllByUser (user)
		if (!rpList || rpList.size() == 0){
			log.info "create new"
			def rp = new RewardPoint()
			rp.user = user 
			log.info ("before " + transaction.amount)
			rp.point = calculatePoint(transaction.amount, null)
			log.info ("after "+ rp.point)	
			rp.save()
		}else{
			log.info "update"
			def rp = rpList.get(0)
			rp.point += calculatePoint(transaction.amount, null)
			rp.save()
		}
		
	}
	
}
